package cn.youyeku.zsxh.Utils;

import android.content.Context;
import android.graphics.Bitmap;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import cn.youyeku.zsxh.app;


/**
 * Created by youye on 17-3-27.
 */

public class FileUtils {

    public static void writeData(String fileName,String data) throws IOException {
        File file = new File(fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(data.getBytes());
        fos.close();


    }

    public static String readDataFromSD(String filePath) throws IOException {

        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        byte[] temp = new byte[1024];
        StringBuilder sb = new StringBuilder("");
        int len = 0;
        //读取文件内容:
        while ((len = fis.read(temp)) > 0) {
            sb.append(new String(temp, 0, len));
        }
        //关闭输入流
        fis.close();
        return sb.toString();
    }

    public static String saveImg(String fileName, Bitmap bm) throws IOException {

        String path = app.getAppContext().getFilesDir().getAbsolutePath() + "/tmp/";

        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
        File myCaptureFile = new File(path + fileName);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
//        Log.w("222", path + fileName);
        return path + fileName;
    }




}
